package com.health.db.dao.user.impl;

import java.util.List;

import com.health.db.dao.ServiceInterface;
import com.health.db.dao.user.IUserAccountJournalDao;
import com.health.db.mapper.UserAccountJournalMapper;
import com.health.db.model.UserAccountJournal;
import com.health.db.model.UserAccountJournalExample;
import com.health.db.model.UserAccountJournalTemp;
import com.health.redis.RedisConstants;

public class UserAccountJournalDaoImpl extends ServiceInterface implements IUserAccountJournalDao {

	UserAccountJournalMapper userAccountJournalMapper;
	String selectByUserId=":userId:";
	String pageStart=":start:";
	String pageEnd=":end:";
	String  offset=":OF:";
	String  pageSize=":PS:";
	
	
	@Override
	public int insert(UserAccountJournal record) {
		// TODO Auto-generated method stub
		getRedisOperatiorLogic();
		
		int result=userAccountJournalMapper.insert(record);
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			
			String keyList = RedisConstants.UserAccountJournalMapper+selectByUserId+record.getAccountid().longValue();
			redisOperatiorLogic.deleteKeys(keyList,  getOrderRedisGroupNo("1"));
		}
		return result;
		
	}
 
     /**
      * 带翻页
      * example
      * {
      * userId    用户id
      * offset    偏移量
      * pageSize  查询条数
      * }
      */
	@Override
	public List<UserAccountJournalTemp> selectUserAccountJournalSimple(
				UserAccountJournalExample example) {
			// TODO Auto-generated method stub
			getRedisOperatiorLogic();
		//	UserAccountJournalMapper userAccountJournalMapper =sqlSession.getMapper(UserAccountJournalMapper.class);
			String key=RedisConstants.UserAccountJournalMapper+selectByUserId+example.getUserId().longValue()
					+offset+example.getOffset()+pageSize+example.getPageSize();
			String keyList = RedisConstants.UserAccountJournalMapper+selectByUserId+example.getUserId().longValue();
			
			Object object=null;
			List<UserAccountJournalTemp> userAccountJournalTemp=null;

			if(redisOperatiorLogic.isJedisIsConnected())
			{	
				//get data from redis
				object = redisOperatiorLogic.readObject(key, getPublicRedisGroupNo("1"));
			 }
			
		     if(null!=object){
		    	if (!object.equals("")) {
		    		 userAccountJournalTemp =  (List<UserAccountJournalTemp>) object;
				}
		     }
		     else{
		         userAccountJournalTemp =userAccountJournalMapper.selectUserAccountJournalSimple(example);
			    if(redisOperatiorLogic.isJedisIsConnected())
				{
					redisOperatiorLogic.setEx(key, userAccountJournalTemp,  getOrderRedisGroupNo("1"));
					redisOperatiorLogic.lpush(keyList, key, getOrderRedisGroupNo("1"));
				}
			 }		
			return userAccountJournalTemp;
		}
		 

	 
		public UserAccountJournalMapper getUserAccountJournalMapper() {
			return userAccountJournalMapper;
		}
		public void setUserAccountJournalMapper(
				UserAccountJournalMapper userAccountJournalMapper) {
			this.userAccountJournalMapper = userAccountJournalMapper;
		}

		/**
		 * 
		 * 按主键删除
		 * 
		 */
		@Override
		public int delete(Long recId,Long accountid) {
			// TODO Auto-generated method stub
			
			getRedisOperatiorLogic();
			
			int result=userAccountJournalMapper.deleteByPrimaryKey(recId);
			if(redisOperatiorLogic.isJedisIsConnected())
			{
				
				String keyList = RedisConstants.UserAccountJournalMapper+selectByUserId+accountid.longValue();
				redisOperatiorLogic.deleteKeys(keyList,  getOrderRedisGroupNo("1"));
			}
			return result;
		}

		/**
		 * 按照对应账单号删除
		 */
		@Override
		public int deleteByBillId(Long billId, Long accountid) {
			
			getRedisOperatiorLogic();
			
			int result=userAccountJournalMapper.deleteByVoucherId(String.valueOf(billId));
			if(redisOperatiorLogic.isJedisIsConnected())
			{
				
				String keyList = RedisConstants.UserAccountJournalMapper+selectByUserId+accountid.longValue();
				redisOperatiorLogic.deleteKeys(keyList,  getOrderRedisGroupNo("1"));
			}
			return result;
			// TODO Auto-generated method stub
		}
}
